package nan.object_oriented;
class Money implements Cloneable{
    public double money=12.5;
    //此时还要将money再克隆一次；
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
//克隆
class Person2 implements Cloneable{//自定义类型要拷贝一定要加Cloneable接口
    public String name;
    public Money m;
    public Person2(String name){//构造方法
        this.name=name;
        this.m=new Money();
    }

    @Override
    public String toString() {//toString方法序列化
        return "Person2{" +
                "name='" + name + '\'' +
                '}';
    }
//重写clone方法
    @Override
    protected Object clone() throws CloneNotSupportedException {//重写克隆方法
        //在此处实现对money克隆的使用
        Person2 person=(Person2) super.clone();//通过Person2先来克隆一个person
            person.m=(Money) this.m.clone();//通过this.m去克隆
            //return super.clone();
            return person;
        }
        //return super.clone();
    }
public class Main5 {
    public static void main(String[] args) throws CloneNotSupportedException{
        Person2 person2=new Person2("nan");
        Person2 person3=(Person2) person2.clone();//clone默认为Object类，强转换为Person类
        System.out.println(person3);
        System.out.println(person2.m.money);
        System.out.println(person3.m.money);
        person3.m.money=18.9;
        System.out.println(person2.m.money);
        System.out.println(person3.m.money);
    }
}
